Attempt to perform seekTo in wrong state

出错情景

1
2
3
4
5
6
7
8
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.pause();
mediaPlayer.seekTo(10 * 1000); // 如果只调用 seek 可正常播放
mainThreadHandler.sendEmptyMessage(MSG_CODE_PLAYER_PREPARED);
}
});
1
2
3
4
5
6
7
8
9
10
11
12
09-27 13:19:14.361 21032-21032/com.lansoeditor.demo E/MediaPlayer: pause called in state 8
09-27 13:19:14.361 21032-21032/com.lansoeditor.demo E/MediaPlayer: error (-38, 0)
09-27 13:19:14.361 21032-21032/com.lansoeditor.demo E/MediaPlayer: Attempt to perform seekTo in wrong state: mPlayer=0xdccaf300, mCurrentState=0
09-27 13:19:14.361 21032-21032/com.lansoeditor.demo E/MediaPlayer: error (-38, 0)
09-27 13:19:14.448 21032-21032/com.lansoeditor.demo E/MediaPlayer: Attempt to perform seekTo in wrong state: mPlayer=0xdccaf300, mCurrentState=0
09-27 13:19:14.448 21032-21032/com.lansoeditor.demo E/MediaPlayer: error (-38, 0)
09-27 13:19:14.450 21032-21032/com.lansoeditor.demo E/MediaPlayer: start called in state 0
09-27 13:19:14.450 21032-21032/com.lansoeditor.demo E/MediaPlayer: error (-38, 0)
09-27 13:19:14.451 21032-21032/com.lansoeditor.demo E/MediaPlayer: Error (-38,0)
09-27 13:19:14.452 21032-21032/com.lansoeditor.demo E/MediaPlayer: Error (-38,0)
09-27 13:19:14.467 21032-21032/com.lansoeditor.demo E/MediaPlayer: Error (-38,0)
09-27 13:19:14.467 21032-21032/com.lansoeditor.demo E/MediaPlayer: Error (-38,0)

如果出现这个错误再调用 mediaPlayer.start() ,会立即产生 MediaPlayer.OnCompletionListener 结束事件

1
2
3
4
5
6
7
8
mplayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
if (mplayer != null && mplayer.isPlaying()) { 防止死循环
rePlay();
}
}
});

参考

android-mediaplyer-seekto-inside-onprepared
MediaPlayer error (-38, 0)